package com.projectName.www.servlet;

import com.projectName.www.service.AdminService;
import com.projectName.www.service.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/adminServlet")
public class AdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        AdminServlet adminServlet = new AdminServlet();
        String func = request.getParameter("func");
        if("1".equals(func)){
            function1(request,response);
        }else if("2".equals(func)){
            function2(request,response);
        }else if("3".equals(func)){
            function3(request,response);
        }else if("4".equals(func)){
            function4(request,response);
        }else if("5".equals(func)){
            function5(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    //查找全部用户功能
    void function1(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.sendRedirect(request.getContextPath() + "/userListServlet");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //查找全部商家功能
    void function2(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.sendRedirect(request.getContextPath() + "/slrListServlet");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //查看所有菜品
    void function3(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.sendRedirect(request.getContextPath() + "/dishListServlet");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 封禁用户
     * 把statu设为1
     */

    void function4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        {
            AdminService as=new AdminServiceImpl();
            as.SetUserStatus(request.getParameter("id"),false);
            response.sendRedirect(request.getContextPath()+"/userListServlet");
        }
    }
    void function5(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        {
            AdminService as=new AdminServiceImpl();
            as.SetUserStatus(request.getParameter("id"),true);
            response.sendRedirect(request.getContextPath()+"/userListServlet");
        }
    }
}
